﻿using System;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace DiscoveryLogic.UI.Charts
{
    internal class XWiseStackedData
    {
        #region Public Methods

        /// <summary>
        /// Initializes a new instance of the Charts.XWiseStackedData class
        /// </summary>
        public XWiseStackedData()
        {
            Positive = new ObservableCollection<DataPoint>();
            Negative = new ObservableCollection<DataPoint>();

            Positive.CollectionChanged += new NotifyCollectionChangedEventHandler(Positive_CollectionChanged);
            Negative.CollectionChanged += new NotifyCollectionChangedEventHandler(Negative_CollectionChanged);

            _isUpdated = false;
        }

        #endregion

        #region Public Properties

        /// <summary>
        /// Collection of DataPoints with positive YValues
        /// </summary>
        public ObservableCollection<DataPoint> Positive
        {
            get;
            set;
        }

        /// <summary>
        /// Collection of DataPoints with negative YValues
        /// </summary>
        public ObservableCollection<DataPoint> Negative
        {
            get;
            set;
        }

        /// <summary>
        /// Positive sum of YValues
        /// </summary>
        public Double PositiveYValueSum
        {
            get
            {
                if (!_isUpdated) Update();
                return _positiveYValueSum;
            }
        }

        /// <summary>
        /// Negative sum of YValues
        /// </summary>
        public Double NegativeYValueSum
        {
            get
            {
                if (!_isUpdated) Update();
                return _negativeYValueSum;
            }
        }

        /// <summary>
        /// Absolute sum of YValues
        /// </summary>
        public Double AbsoluteYValueSum
        {
            get
            {
                return Math.Abs(NegativeYValueSum) + Math.Abs(PositiveYValueSum);
            }
        }

        #endregion

        #region Private Methods

        /// <summary>
        /// Event handler manages the addition and removal of DataPoints from positive collection of DataPoints 
        /// </summary>
        private void Positive_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
        {
            _isUpdated = false;
        }

        /// <summary>
        /// Event handler manages the addition and removal of DataPoints from negative collection of DataPoints 
        /// </summary>
        private void Negative_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
        {
            _isUpdated = false;
        }

        /// <summary>
        /// Update sum of YValues
        /// </summary>
        private void Update()
        {
            _isUpdated = true;
            _positiveYValueSum = (from dataPoint in Positive where !Double.IsNaN(dataPoint.InternalYValue) select dataPoint.InternalYValue).Sum();
            _negativeYValueSum = (from dataPoint in Negative where !Double.IsNaN(dataPoint.InternalYValue) select dataPoint.InternalYValue).Sum();
        }

        #endregion

        /// <summary>
        /// Positive sum of YValues
        /// </summary>
        private Double _positiveYValueSum;

        /// <summary>
        /// Negative sum of YValues
        /// </summary>
        private Double _negativeYValueSum;

        /// <summary>
        /// Whether the x wise data is updated
        /// </summary>
        private Boolean _isUpdated;

    }
}
